package org.karelun.gef.outline;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.gef.editparts.AbstractTreeEditPart;
import org.karelun.gef.model.AttributeModel;
import org.karelun.gef.model.ClassModel;
import org.karelun.gef.model.MethodModel;

public class ClassModelTreeEditPart extends AbstractTreeEditPart implements PropertyChangeListener{
	
	public ClassModelTreeEditPart(Object model){
		super(model);
	}
	
	
	@SuppressWarnings("unchecked")
	protected List getModelChildren() {
		ClassModel classModel = getClassModel();
		List<AttributeModel> attrModels = classModel.getAttrModels();
		List<MethodModel> methodModels = classModel.getMethodModels();
		List<Object> list = new ArrayList<Object>();
		list.addAll(attrModels);
		list.addAll(methodModels);
		if(list.isEmpty())
			return list;
		return super.getModelChildren();
	}
	
	@Override
	protected void refreshVisuals() {
		ClassModel classModel = getClassModel();
		setWidgetText(classModel.getText());
	}
	
	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if(ClassModel.PROP_ATTRIBUTE.equals(evt.getPropertyName()))
			refreshVisuals();
	}
	
	@Override
	public void activate() {
		super.activate();
		getClassModel().addPropertyChangeListener(this);
	}
	
	@Override
	public void deactivate() {
		super.deactivate();
		getClassModel().removePropertyChangeListener(this);
	}
	
	private ClassModel getClassModel(){
		return (ClassModel)getModel();
	}
	
}
